/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.Shop.controller;

import com.Shop.beans.Document;
import com.Shop.beans.OdbsException;
import com.Shop.beans.User;
import com.Shop.service.UserService;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author cjh
 */
@Controller
@SessionAttributes("loginUser")
public class UserController {

    @Resource(name = "userService")
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
/**
 * 添加用户
 * @param model
 * @return 
 */
   @RequestMapping(value ="/addUser",method = RequestMethod.GET)
   public String addUser(Model model){
       return "user/register";
   }
   
   
   @RequestMapping(value ="/addUser",method = RequestMethod.POST)
   public String addUser(String phone,String password,String nickname){
       userService.addUser(phone, password,nickname);
//       System.out.println(phone);
       return "redirect:/addUser";
   }
   
   /**
    * 删除用户
     * @param id
     * @return 
    */
   @RequestMapping(value="/list/{id}",method=RequestMethod.GET)
   public String deleteUser(@PathVariable int id){
//       if(loginUser!=null){
//           if(loginUser.getType()==1)
               userService.deleteUser(id);
//       }
       return "redirect:addUser";
   }
   
   /**
    * 登录用户
    * 
    */
   @RequestMapping(value="/login",method=RequestMethod.GET)
   public String loginUser(Model model){
       return "user/login";
   }
   
   @RequestMapping(value="/login",method=RequestMethod.POST)
   public String loginUser(Model model,String phone,String password){
       User u=userService.loginUser(phone, password);
       if(u!=null){
           model.addAttribute("loginUser",u);
//           System.out.println(u.getPhone());
       }
       return "redirect:/update";
   }
   
   /**
    * 修改用户信息
    */
   @RequestMapping(value="/update",method=RequestMethod.GET)
   public String updateUser(Model model,@ModelAttribute("loginUser")User loginUser){
       if(loginUser==null){
           return "user/login";
       }
       return "user/update";
   }
   
   @RequestMapping(value="/update",method=RequestMethod.POST)
   public String updateUser(@ModelAttribute("loginUser") User loginUser,String password,String Email,String sex,String nickname,@RequestParam(value = "file") MultipartFile file,HttpServletRequest request){
       loginUser.setEmail(Email);
       loginUser.setSex(sex);
       loginUser.setNickname(nickname);
       loginUser.setPassword(password);
       
       if(file!=null){
       String path =request.getSession().getServletContext().getRealPath("app/img/user");
       String fileName=file.getOriginalFilename();
       System.out.println(path);
       File targetFile =new File(path,fileName);
       if(targetFile.exists()){
           targetFile.mkdirs();
       }
       try{
           file.transferTo(targetFile);
       }catch(Exception e){
           e.printStackTrace();
       }
       loginUser.setImg(fileName);}
       userService.updateUser(loginUser);
       return "user/update";
   }
   @RequestMapping(value="/upload",method=RequestMethod.GET)
  public String uploadImage(Model model){
      return "user/upload";
  }
  
  @RequestMapping(value="upload",method=RequestMethod.POST)
   public String uploadImage(@RequestParam(value = "file") MultipartFile file,HttpServletRequest request){
       String path =request.getSession().getServletContext().getRealPath("img");
       String fileName=file.getOriginalFilename();
       System.out.println(path);
       File targetFile =new File(path,fileName);
       if(targetFile.exists()){
           targetFile.mkdirs();
       }
       try{
           file.transferTo(targetFile);
       }catch(Exception e){
           e.printStackTrace();
       }
       return "user/upload";
   }
}
